package nettypro.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @ClassName NIOFileChannel01
 * @Description TODO
 * @Author zeny
 * @Date 2020/3/21 0021 21:33
 */
public class NIOFileChannel03 {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\project\\datastructures\\demo.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel channel01 = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel channel02 = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
        int len  = 0;
        while (true) {
            byteBuffer.clear();
            int read = channel01.read(byteBuffer);
            if (read != -1) {
                byteBuffer.flip();
                channel02.write(byteBuffer);
            }else {
                break;
            }
        }
        fileOutputStream.close();
        fileInputStream.close();
    }
}
